# -*- coding: utf-8 -*-

from pages.basePage import BaseHandler
import jinja2
import os
from models.usuario import Usuario
from models.persona import Persona

from google.appengine.ext import db
from google.appengine.api import images

jinja_environment = jinja2.Environment(
    loader=jinja2.FileSystemLoader(os.path.dirname(__file__)))

class CriarPersona(BaseHandler):
    def get(self):
        userId = self.session.get('userid')        
        user = None
        if userId is not None:
            user = Usuario.get_by_id(userId)
        if user is None:
            self.redirect('/')
            return
        url = self.session.get('url')
        if url is None:
            self.redirect('/')
            return
        
        vincc = self.request.get('vincc')
        
        template_values = {
            'vincc' : vincc,
            'user' : user,
            'url' : url
        }
        
        template = jinja_environment.get_template('html/criarpersona.html')
        self.response.out.write(template.render(template_values))
        
    def post(self):
        userId = self.session.get('userid')        
        user = None
        if userId is not None:
            user = Usuario.get_by_id(userId)
        if user is None:
            self.redirect('/')
            return
        
        nome = self.request.get('nome')
        if nome is None:
            nome = ''
            
        idade = self.request.get('idade')
        if idade:
            idade = int(idade)
        else:
            idade = 1
        
        sexo = self.request.get('sexo')
        if sexo is None:
            sexo = 'Masculino'
        else:
            if sexo == '1':
                sexo = 'Masculino'
            else:
                sexo = 'Feminino'
            
        descricao = self.request.get('descricao')
        if descricao is None:
            descricao = ''
        
        personaSessao = {'nome' : nome,
                         'descricao' : descricao,
                         'idade' : idade,
                         'sexo' : sexo,
                         'acaoReflexao' : int(self.request.get('acaoreflexao')),
                         'monoPolicromatico' : int(self.request.get('monopoli')),
                         'coletivoIndividual' : int(self.request.get('coleindi'))}
        
        self.session['personaSessao'] = personaSessao        
        
        """
        img = self.request.get('img')
        imagem = None
        if img is not None and len(img) > 0:
            imagem = images.resize(img, 200, 200)
            
        persona = Persona()
                                     
        persona.nome = nome
        persona.descricao = descricao
        persona.idade = idade
        persona.sexo = sexo
        persona.acaoReflexao = long(self.request.get('acaoreflexao'))
        persona.monoPolicromatico = long(self.request.get('monopoli'))
        persona.coletivoIndividual = long(self.request.get('coleindi'))
        persona.imagem = db.Blob(imagem)
        
        persona.put()
        """
        
        vincc = self.request.get('vincc')
        
        redirect_url = '/escolherMecanica?vinc=1'
        if vincc:
            redirect_url = redirect_url + '&vincc=1'
        
        self.redirect(redirect_url)
        
class CriarPersona2(BaseHandler):
    def get(self):
        userId = self.session.get('userid')        
        user = None
        if userId is not None:
            user = Usuario.get_by_id(userId)
        if user is None:
            self.redirect('/')
            return
        url = self.session.get('url')
        if url is None:
            self.redirect('/')
            return
        
        vincc = self.request.get('vincc')
        
        pid = self.request.get('pid')
        
        template_values = {
            'vincc' : vincc,
            'user' : user,
            'url' : url,
            'pid' : pid
        }
        
        template = jinja_environment.get_template('html/criarpersonaFoto.html')
        self.response.out.write(template.render(template_values))
        
    def post(self):
        userId = self.session.get('userid')        
        user = None
        if userId is not None:
            user = Usuario.get_by_id(userId)
        if user is None:
            self.redirect('/')
            return
        
        vincc = self.request.get('vincc')
        
        pid = self.request.get('pid')
        
        img = self.request.get('img')
        imagem = None
        if img is not None and len(img) > 0:
            imagem = images.resize(img, 200, 200)
            
        persona = Persona.get_by_id(long(pid))
        
        if persona:
            persona.imagem = db.Blob(imagem)
            persona.put()
            
            if vincc:                
                cenarioSessao = self.session.get('cenarioSessao')
                if cenarioSessao is None:
                    self.redirect('/cenario')
                    return
                        
                self.redirect('/cenario?vincc=1&pid=' + str(persona.key().id()))
                return
            
        self.redirect('/')